<?php	
	/*\
	 *	Sample Module template for "OH"
	 *  
	 *  use for write develop module interface
	 *  to encampusulate opensource script into "OH"
	\*/
	
	// Interface function	
	function oh_module_paginator($tokens) {
		return oh_this();
	} 

	// Option file required
	//require_once($oh['path']."/modules/mail/libmail.php");
	
	// Main class of module
	class oh_class_paginator {
		public $paginator;
		function oh_class_paginator() {
			$this->paginator = array();
		}
		function define($get,$ppp,$tot) {
			$this->paginator['get'] = $get;
			$this->paginator['pag'] = isset($_GET[$get]) ? ((int)$_GET[$get]) : (0);
			$this->paginator['tot'] = $tot;
			$this->paginator['ppp'] = $ppp;
			$this->paginator['off'] = $this->paginator['pag'] * $ppp;
			$this->paginator['nop'] = oh_math_integer_divide($tot,$ppp);
		}
		function off() {
			return $this->paginator['off'];
		}
		function ppp() {
			return $this->paginator['ppp'];
		}
		function pag() {
			return isset($_GET["p"]) ? ((int)$_GET["p"]) : (0);
		}
		function bar() {
			$pag = $this->paginator['pag'];
			$nop = $this->paginator['nop'];
			if ($nop>0) {
				echo '<div class="paginator">';
				$button = array();
				if ($pag>0) {
					$button[] = '<a href="'.$this->url($pag-1).'">&lt;&lt;</a>';
				}
				for($i=0;$i<=$nop;$i++) {
					$button[] = '<a href="'.$this->url($i).'">'.($i+1).'</a>';				
				}
				if ($pag<$nop) {
					$button[] = '<a href="'.$this->url($pag+1).'">&gt;&gt;</a>';
				}
				echo implode(" ",$button);
				echo '</div>';
			}
		}
		function url($pag) {
			$get = $this->paginator['get'];
			$url = oh("url");
			$tok = parse_url($url);
			$quy = $tok["query"];
			parse_str($quy,$str);
			$str[$get] = $pag;
			$quy = http_build_query($str);
			$tok['query'] = $quy;
			$url = $tok["scheme"]."://".$tok["host"].$tok["path"]."?".$quy;		
			return $url;
		}
	}
	
	// Main object of module
	global $oh_object_paginator;
	$oh_object_paginator = new oh_class_paginator();
?>
